﻿using Developer.Code;
using Developer.Entity;
using Developer.Service;
using Super.Framework.Common;
using Super.Framework.Common.Web;
using Super.Framework.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Developer.Web.Areas.Admin.Controllers
{
    public class OrderController : BaseController
    {
        // GET: Admin/Order
        public ActionResult Index()
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var key = Query.Value("name");
            int pageCount;
            int itemCount;
            var users = OrderService.GetAll().Where(a => a.State == 0 || a.State == 1);

            if (key.IsNotNullOrEmpty())
            {
                users = users.Where(a => a.Num.Contains(key));
                ViewBag.Name = key;
            }
            var items = users.OrderByDescending(u => u.CreateTIme).OrderBy(u => u.State)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex);

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
            return View(items);
        }

        public ActionResult Detail()
        {
            var num = Query.Value("num");
            var order = OrderService.GetAll().Where(a => a.Num == num);

            ViewBag.UserAccount = UserService.GetAll().Find(order.First().UserId).Account;
            ViewBag.UserId = order.First().UserId;
            return View(order);
        }
        
        public ActionResult SetOrderDone()
        {
            var num = Query.Value("num");
            var orders = OrderService.GetAll().Where(a => a.Num == num);
            if (orders != null)
            {
                foreach (var order in orders)
                {
                    if (order.State == 0)
                    {
                        order.State = 1;
                    }
                    else
                    {
                        order.State = 0;
                    }
                }
                SysInfo.DB.SaveChanges();
                ShowMsg("操作成功", MessageItemType.Right);
                return Redirect("/admin/order/index");

            }
            ShowMsg("操作失败", MessageItemType.Error);
            return Redirect("/admin/order/index");
        }
    }
}